#include "Registration.h"
#include "ui_Registration.h"
#include <QHeaderView>
#include <QDebug>
#include "MainMenu.h"
Registration::Registration(QRect screenGeometry,QString medicals,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Registration)
{
    ui->setupUi(this);
    this->screenGeometry=screenGeometry;
    analysisMedical(medicals);
    init();
}

Registration::~Registration()
{
    qDebug()<<"进入了Registration析构函数"<<endl;
    for(int i=0;i<items.size();i++)
    {
        for(int j=0;j<items[i].size();j++)
            delete items[i][j];
    }
    delete ui;
}

void Registration::init()
{
    this->registTable=new QTableWidget(this);
    setRegistTableMode();
    this->exit=new QPushButton(this);
    this->exit->setGeometry(0,this->screenGeometry.height()-300,this->screenGeometry.width(),300);
    connect(this->registTable,&QTableWidget::cellClicked,this,&Registration::cellCilcked_slots);
    connect(this->exit,&QPushButton::clicked,[=](){
        this->close();
        delete this;
    });
    this->show();
}

void Registration::analysisMedical(QString str)
{//QString str="[name(Calculus operation),currentLine(4),isLine(false)],[name(other operation),currentLine(4),isLine(false)];";
    qDebug()<<"str"<<str<<endl;
    QRegularExpression reg_name("(?<=name\\().*?(?=\\))");
    QRegularExpressionMatchIterator iter_name = reg_name.globalMatch(str);

    QRegularExpression reg_currentLine("(?<=currentLine\\().*?(?=\\))");
    QRegularExpressionMatchIterator iter_currentLine = reg_currentLine.globalMatch(str);

    QRegularExpression reg_isLine("(?<=isLine\\().*?(?=\\))");
    QRegularExpressionMatchIterator iter_isLine = reg_isLine.globalMatch(str);

    QRegularExpression reg_myNumber("(?<=number\\().*?(?=\\))");
    QRegularExpressionMatchIterator iter_myNumber = reg_myNumber.globalMatch(str);

        /* 遍历查找匹配。 */
        while (iter_name.hasNext() && iter_currentLine.hasNext() && iter_isLine.hasNext()) {
            medicalKind m;
            m.name=iter_name.next().captured(0);
            m.currentLine=iter_currentLine.next().captured(0).toInt();
            m.isLine=iter_isLine.next().captured(0)=="true"?true:false;
            if(m.isLine)
            {
                m.myNumber=iter_myNumber.next().captured(0);
                qDebug()<<"找到的number:"<<m.myNumber<<endl;
            }
            else
                m.myNumber="";
            this->medicals.append(m);
        }
}

void Registration::setRegistTableMode()
{
    QFont font;
    font.setPixelSize(20);
    this->registTable->setFixedSize(QSize(screenGeometry.width(),this->screenGeometry.height()-300));
    this->registTable->setRowCount(medicals.size());
    this->registTable->setColumnCount(4);
    QStringList headNames={"医疗服务","前方人数","预约/取消","号码"};
    this->registTable->setHorizontalHeaderLabels(headNames);
    this->registTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    this->registTable->horizontalHeader()->setFont(font);
    this->registTable->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    //this->registTable->resizeColumnsToContents();
   //this->registTable->resizeRowsToContents();

    for(int i=0;i<medicals.size();i++)
    {
        QList<QTableWidgetItem*> list;

        list.append(new QTableWidgetItem(medicals[i].name));//医疗服务名
        this->registTable->setItem(i,0,list[0]);
        this->registTable->item(i,0)->setTextAlignment(Qt::AlignHCenter);
        this->registTable->item(i,0)->setFont(font);

        list.append(new QTableWidgetItem(QString::number(medicals[i].currentLine)));//前方排队人数
        this->registTable->setItem(i,1,list[1]);
        this->registTable->item(i,1)->setTextAlignment(Qt::AlignHCenter);
        this->registTable->item(i,1)->setFont(font);

        list.append(new QTableWidgetItem(medicals[i].isLine?"取消预约":"立即预约"));//是否预约
        this->registTable->setItem(i,2,list[2]);
        this->registTable->item(i,2)->setTextAlignment(Qt::AlignHCenter);
        this->registTable->item(i,2)->setFont(font);
        qDebug()<<"medicals[i].isLine:"<<medicals[i].isLine<<endl;
        if(medicals[i].isLine)
            list.append(new QTableWidgetItem(medicals[i].myNumber));
        else
            list.append(new QTableWidgetItem(""));
        this->registTable->setItem(i,3,list[3]);
        qDebug()<<"list[3]:"<<list[3]->text()<<endl;
        this->registTable->item(i,3)->setTextAlignment(Qt::AlignHCenter);
        this->registTable->item(i,3)->setFont(font);
        this->items.append(list);
    }
    this->registTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void Registration::cellCilcked_slots(int row, int column)
{qDebug()<<"row:"<<row<<"   column:"<<column<<endl;
    if(column==2)
    {
        QString str("7E7E;kind:phone;function:03;phone:%1;pass:%2;choose:%3(%4);");
        //qDebug()<<"str------------:"<<str.arg(MainMenu::phone).arg(MainMenu::pass).arg(this->medicals[row].name).arg(this->medicals[row].isLine?"false":"true")<<endl;
        emit toServerSend_signals(str.arg(MainMenu::phone).arg(MainMenu::pass).arg(this->medicals[row].name).arg(this->medicals[row].isLine?"false":"true"));
    }
}

void Registration::receiveServer_slot(int function, QString data)
{qDebug()<<"data:"<<data<<endl;
    switch (function) {
    case 3://Calculus operation(true),number(45),currentLine(4)
        QRegularExpression reg_mediaName("^.*?(?=\\()");//医疗服务名
        QRegularExpression reg_number("(?<=number\\()[^()]*?(?=\\))");//号码
        QRegularExpression reg_currentLine("(?<=currentLine\\()[^()]*?(?=\\))");//前方排队人数
        QRegularExpressionMatchIterator iter_mediaName = reg_mediaName.globalMatch(data);
        QRegularExpressionMatchIterator iter_currentLine = reg_currentLine.globalMatch(data);
        QRegularExpressionMatchIterator iter_number = reg_number.globalMatch(data);
            /* 遍历查找匹配。 */
            if(iter_mediaName.hasNext() && iter_currentLine.hasNext()) {
                QString name=iter_mediaName.next().captured(0);//获取医疗名
                if(data.indexOf(name+"(true)")>=0)//说明修改成功
                {
                    for(int i=0;i<this->medicals.size();i++)
                    {qDebug()<<"name:"<<this->medicals[i].name<<endl;
                        if(this->medicals[i].name==name)
                        {
                            this->medicals[i].isLine?this->medicals[i].isLine=false:this->medicals[i].isLine=true;//修改当前是否取号
                            if(this->medicals[i].isLine==false)//修改当前取的号码
                                this->medicals[i].myNumber="";
                            else
                                this->medicals[i].myNumber=iter_number.next().captured(0);
                            this->medicals[i].currentLine=iter_currentLine.next().captured(0).toInt();//修改前方排队人数

                            this->medicals[i].isLine?this->items[i][2]->setText("取消预约"):this->items[i][2]->setText("立即预约");
                            this->items[i][1]->setText(QString::number(this->medicals[i].currentLine));
                            this->items[i][3]->setText(this->medicals[i].myNumber);
                            break;
                        }
                    }
                }
        }
        break;
    }
}
